Skip to main content

Chapter 31 - Publish Modules to Terraform Public Registry

Let's publish that static website to the registry

Create Github Repo


From the Git repo, folder 52.

  • Click on Create a new repository
  • Follow Naming Conventions for modules
    • terraform-PROVIDER-MODULE_NAME
    • Sample: terraform-azurerm-staticwebsitepublic
  • Repository Name: terraform-azurerm-staticwebsitepublic
  • Description: Terraform Modules to be shared in Terraform Public Registry
  • Repo Type: Public
  • Initialize this repository with:
  • UN-CHECK - Add a README file
  • CHECK - Add .gitignore
  • Select .gitignore Template: Terraform
  • CHECK - Choose a license
  • Select License: Apache 2.0 License (Optional)
  • Click on Create repository

Pull down repo


  1. Pull down the repository and add it to your VSCode workspace.
    image.png

Create the module.


  1. Copy all of the files from the modules folder as you have defined in the last chapter into this new folder
  2. Commit and push. Note: You may need to create a token and authenticate in VSCode. https://github.com/settings/tokens

Create new release tag in repo


  1. Go to the Repo in Github and create a new release.

  2. Tag it v1.0.0, add a title and a release and then create the release

image.png

Publish to TF Registry


  1. Go to the TF registry, sign in, publish and select your repo.
  2. Easy as that.

https://registry.terraform.io/modules/Dupo24/staticwebsite/azurerm/latest

image.png

Call the module from your root TF


resource-staticwebsite.tf


# Use the custom TF Module
module "staticwebsite" {
source = "Dupo24/staticwebsite/azurerm"
version = "1.0.1"

# Resource Group
location = "centralus"
resource_group_name = "dupo_rg1"

# Storage Account
storage_account_name = "dupostaticwebsite"
storage_account_tier = "Standard"
storage_account_replication_type = "LRS"
storage_account_kind = "StorageV2"
static_website_index_document = "index.html"
static_website_error_404_document = "error.html"
}

outputs.tf


# Output variable definitions
output "resource_group_id" {
description = "resource group id"
value = module.azure_static_website.resource_group_id
}
output "resource_group_name" {
description = "The name of the resource group"
value = module.azure_static_website.resource_group_name
}
output "resource_group_location" {
description = "resource group location"
value = module.azure_static_website.resource_group_location
}
output "storage_account_id" {
description = "storage account id"
value = module.azure_static_website.storage_account_id
}
output "storage_account_name" {
description = "storage account name"
value = module.azure_static_website.storage_account_name
}

variables.tf


versions.tf


# Terraform Block
terraform {
required_version = ">= 1.0.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "< 4.0"
}
}
}

# Provider Block
provider "azurerm" {
features {}
}


Terraform init, validate, plan, apply, destroy


  1. Follow the terraform lifecycle to create your resources

image.png

image.png

image.png

Managing your module via the TF Registry


Manage Module button is used to make some changes to your module.

Updating the module


  1. Make a change to the module, test, etc
  2. Create a new release, increment the version
  3. Commit and push to Github
  4. Terraform Registry will pick up the latest changes from the github.

image.png